app.controller('chargeRuleCtrl',['$scope','toaster','utilService','chargeRuleService','$modal',
    function($scope,toaster,utilService,chargeRuleService,$modal){

    $scope.chargeRule={};

    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'charge/rule',
            params:$scope.chargeRule
        }).then(function(result){
        });
    };
    $scope.query();

    //删除会员卡储值规则
    $scope.del = function(id){
        // query data.
        openModal("确定要删除该储值规则吗？");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"message",
                controller:"deleteCtrl",
                message:msg,
                data:id
            });
        }
    };
}]);

app.controller('chargeRuleAddCtrl', ['$scope','$state','chargeRuleService','toaster',
    function($scope,$state,chargeRuleService,toaster) {

        $scope.chargeRuleVO={};
        $scope.addRule = function() {
            chargeRuleService.addRule($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $state.go('app.memberCharge');
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };

        //获取该商家优惠券
        $scope.coupons={};
        $scope.findCoupons = function() {
            chargeRuleService.findCoupons($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.couponsList = result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.findCoupons();
    }
]);

app.controller('chargeRuleUpdateCtrl', ['$scope','$state','chargeRuleService','toaster','$stateParams',
    function($scope,$state,chargeRuleService,toaster,$stateParams) {

        $scope.chargeRuleVO={
            id:$stateParams.id
        };
        $scope.updateRule = function() {
            chargeRuleService.updateRule($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $state.go('app.memberCharge');
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };

        //获取该规则详情
        $scope.findById = function() {
            chargeRuleService.findById($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.chargeRuleVO = result.data;
                        if($scope.chargeRuleVO.giveCouponsId!=null){
                            $scope.chargeRuleVO.isGiveCoupons=1;
                        }
                        if($scope.chargeRuleVO.giveIntegral!=null){
                            $scope.chargeRuleVO.isGiveIntegral=1;
                        }
                        if($scope.chargeRuleVO.giveMoney!=null){
                            $scope.chargeRuleVO.isGiveMoney=1;
                        }
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.findById();

        //获取该商家优惠券
        $scope.coupons={};
        $scope.findCoupons = function() {
            chargeRuleService.findCoupons($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.couponsList = result.data;
                        break;
                    case 624:
                        toaster.pop('error', "操作失败", "找不到该店铺,请刷新重试");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.findCoupons();
    }
]);

app.controller('deleteCtrl', ['$scope', '$modalInstance','$state','chargeRuleService','toaster','modalData',
    function($scope, $modalInstance,$state,chargeRuleService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.id = modalData.data;
        $scope.isDelModal=true;

        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            chargeRuleService.deleteRule($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);
